<?php
namespace app\wx\controller;
use think\facade\Request;
use app\wx\validate\UserValidate;
use app\wx\service\UserService;

//用户管理
class User
{
    //查询
    public function findUser()
    {
        $page = Request::post('page/d', 1); //当前页
        $limit = Request::post('limit/d', 10); //每页数量
        $field = Request::post('field/s', "*"); //寻找字段
        //接收参数post,查询条件待定
        $param = array(
            'userid'          => Request::post('userid/s',''), //USERID
            'name'            => Request::post('name/s',''), //名字
            'mobile'          => Request::post('mobile/s',''), //手机
            'gender'          => Request::post('gender/d',''), //性别 0 未知 1 男 2 女
            'email'           => Request::post('email/s',''), //用户邮箱
            'alias'           => Request::post('alias/s',''), //用户别名
            'main_department' => Request::post('main_department/d',''), //主部门ID
            'start_time' => Request::post('startTime/s', ''), //开始时间
            'end_time' => Request::post('entTime/s', '') //结束时间
        );
        $data = UserService::findUser($param,$page,$limit,$field);
        return success($data);
    }

    //添加接口
    public function add()
    {
        //接受参数
        $param = array(
            'userid'            => Request::post('userid/s',''), //USERID
            'name'              => Request::post('name/s',''), //名字
            'alias'             => Request::post('alias/s',''), //别名
            'mobile_area_code'  => Request::post('mobile_area_code/s',''), //手机区号
            'mobile'            => Request::post('mobile/s',''), //手机
            'email'             => Request::post('email/s',''), //用户邮箱
            'department'        => Request::post('department/a',[]), //所属部门ID
            'order'             => Request::post('order/a',[]), //部门内的排序值
            'position'          => Request::post('position/s',''), //职务信息
            'gender'            => Request::post('gender/d',0), //性别 0 未知 1 男 2 女
            'telephone'         => Request::post('telephone/s',''), //座机
            'is_leader_in_dept' => Request::post('is_leader_in_dept/a',[]), //部门内是否为上级
            'avatar_mediaid'    => Request::post('avatar_mediaid/s', ''), //成员头像的mediaid
            'enable'            => Request::post('enable/d', 1), //1：启动，2：禁用
            'extattr'           => Request::post('extattr/s', ''), //自定义字段
            'to_invite'         => Request::post('to_invite/s', ''), //是否邀请该成员使用企业微信
            'external_profile'  => Request::post('external_profile/s', ''), //成员对外属性
            'external_position' => Request::post('external_position/s', ''), //对外职务
            'external_position' => Request::post('external_position/s', ''), //对外职务
            'address'           => Request::post('address/s',''), //地址
            'main_department'   => Request::post('main_department/d',''), //主部门
        );
        if((!isset($param['mobile']) || empty($param['mobile'])) && (!isset($param['email']) || empty($param['email']))){
            return error("邮箱和手机不能同时为空！");
        }
        $validate = new UserValidate();
        if (!$validate->scene('add')->check($param)) {
            return error($validate->scene('add')->getError());
        }
        $id = UserService::add($param);
        return success([
            'id' => $id
        ]);
    }

    //修改接口
    public function edit()
    {
        //接受参数
        $param = array(
            'id'                => Request::post('id/s',''), //ID
            'userid'            => Request::post('userid/s',''), //USERID
            'name'              => Request::post('name/s',''), //名字
            'alias'             => Request::post('alias/s',''), //别名
            'mobile_area_code'  => Request::post('mobile_area_code/s',''), //手机区号
            'mobile'            => Request::post('mobile/s',''), //手机
            'email'             => Request::post('email/s',''), //用户邮箱
            'department'        => Request::post('department/a',[]), //所属部门ID
            'order'             => Request::post('order/a',[]), //部门内的排序值
            'position'          => Request::post('position/s',''), //职务信息
            'gender'            => Request::post('gender/d',0), //性别 0 未知 1 男 2 女
            'telephone'         => Request::post('telephone/s',''), //座机
            'is_leader_in_dept' => Request::post('is_leader_in_dept/a',[]), //部门内是否为上级
            'avatar_mediaid'    => Request::post('avatar_mediaid/s', ''), //成员头像的mediaid
            'enable'            => Request::post('enable/d', 1), //1：启动，2：禁用
            'extattr'           => Request::post('extattr/s', ''), //自定义字段
            'to_invite'         => Request::post('to_invite/s', ''), //是否邀请该成员使用企业微信
            'external_profile'  => Request::post('external_profile/s', ''), //成员对外属性
            'external_position' => Request::post('external_position/s', ''), //对外职务
            'external_position' => Request::post('external_position/s', ''), //对外职务
            'address'           => Request::post('address/s',''), //地址
            'main_department'   => Request::post('main_department/d',''), //主部门
        );
        if((!isset($param['mobile']) || empty($param['mobile'])) && (!isset($param['email']) || empty($param['email']))){
            error("邮箱和手机不能同时为空！");
        }
        $validate = new UserValidate();
        if (!$validate->scene('edit')->check($param)) {
            error($validate->scene('edit')->getError());
        }
        UserService::edit($param);
        return success();
    }

    //同步接口
    public function asyncUser()
    {
        set_time_limit(0);
        //获取微信token
        $token_url = Config::get('wx.get_token_url');
        $corpid = Config::get('wx.corpid');
        $corpsecret = Config::get('wx.corpsecret');
        $result = http_get($token_url.'?corpid='.$corpid.'&corpsecret='.$corpsecret);
        $result = json_decode($result, true);
        if($result['errcode'] !== 0)
            error($result['errmsg']);
        //获取部门数据
        
    }
}
